<script type="text/javascript">
var curImg = "curImg";
var lastcardid="";
$(function(){	
	loadTable();
	$("input[name='1']").bind("change",loadTable);
	loadUserInfo();	
	$("#btnNext").bind("click",save);	
	$("#fileHead").bind("change",loadImg);
	bindSubmit();
});


function loadTable(){
	var cardid = $("input[name='1']:checked").val();
	var imgsrcs = getImgSrcs();
	var params = {
				cardid:cardid,
				imgsrcs:imgsrcs,
				lastcardid:lastcardid
			};
	lastcardid = cardid;
	
	$.ajax({
		type:"post",
		url:"index.php/login/getCardTable",
		dataType:"json",
		data:params,
		success:function(data){
			$("#tableMain").html(data.card);
			var tableClass = $("#tableMain table:first").attr("class");
			
			bindImgFile(tableClass);
		}
	});
}
function getImgSrcs(){
	var imgSrcs = "";
	$("#tableMain img").each(function(){
		var src = $(this).attr("src");
		if(src.indexOf("images/")==-1)
			imgSrcs+=$(this).attr("src")+";";
	});
	return imgSrcs;
}
function loadUserInfo(){
	$.ajax({
		type:"post",
		url:"index.php/login/getUserInfo",
		dataType:"json",
		success:function(data){			
			if(!IsEmpty(data.cardid)){
				$("input[name='1'][value='"+data.cardid+"']").trigger("click");
				lastcardid=data.cardid;
			}else{
				lastcardid=1;
			}
			if(!IsEmpty(data.cardcontent))
				$("#tableMain").html(data.cardcontent);						
		}
	});
}


function save(){
	var cardid = $("input[name='1']:checked").val();
	var cardcontent = $("#tableMain").html();
	var params = {
				cardid:cardid,
				cardcontent:cardcontent
			};
	var url = "index.php/login/saveCard";
	$.ajax({
		type:"post",
		url:url,
		dataType:"json",
		data:params,
		success:function(data){
			if(data.ReturnCode==1){
				window.location="<?php echo base_url()?>index.php/login/show"
			}else{
				alert('保存失败');
			}
		}
	});
}

function bindSubmit(){	
	var options = {  
	        beforeSubmit:  showRequest,  //提交前处理 
	        success:       showResponse,  //处理完成 
	        resetForm: false,  
	        dataType:  'json'  
	    };  
	  
	    $('#formCard').submit(function() {
		    
	        $(this).ajaxSubmit(options);
	        return false;
	    });  
}
function showRequest(formData, jqForm, options) { 
    return true;  
}  

function showResponse(data, statusText)  {	
    if(data.ReturnCode==1){
        $("."+curImg).attr("src",data.src);        
    }else{
        alert(data.Msg);
    }
    return false;   
} 

function bindImgFile(cname){	
	$("."+cname+" img").bind("click",function(){
		$(this).parents("table").find("img").removeClass(curImg);
		$(this).addClass(curImg);
		$("#fileHead").trigger("click");
	});
}

function loadImg(){
	var fileName = $("#fileHead").val();
	var ext=fileName.substr(fileName.indexOf(".")).toLowerCase();
	if(ext!=".gif" && ext!=".jpg" && ext!=".jpeg" && ext!=".png" && ext!=""){
		alert("图片格式无效");		
		return;
	}
	if(!IsEmpty(fileName)){	
		$("#formCard").submit();
		return;
	}
}
</script>
<body>
<?php
//include 'headyd.php';
?>
	<form id="formCard" action="index.php/login/uploadImg" method="post"
		enctype="multipart/form-data">

		<!-- 04 开始 -->
		<div class="yindao_k">
			<div class="yindao_bt">
				<img src="images/yindao_04.jpg" />
			</div>
			<input type="file" id="fileHead" name="mypic" style="display: none">

			<!-- 模特卡管理 开始 -->
			<div class="moteka_mb">
				<ul>
					<li><img src="images/moban_01.jpg" /> <br /> <span><input id="r1"
							name="1" type="radio" value="1" checked="checked" />10幅（竖版）</span></li>
					<li><img src="images/moban_02.jpg" /> <br /> <span><input name="1"
							type="radio" value="2" />10幅（竖版）</span></li>
					<li><img src="images/moban_03.jpg" /> <br /> <span><input name="1"
							type="radio" value="3" />10幅（竖版）</span></li>
					<li><img src="images/moban_04.jpg" /> <br /> <span><input name="1"
							type="radio" value="4" />10幅（竖版）</span></li>
					<li><img src="images/moban_05.jpg" /> <br /> <span><input name="1"
							type="radio" value="5" />10幅（竖版）</span></li>
				</ul>
			</div>

			<div class="moban">
				<div id='tableMain' class='divCard'></div>
			</div>

			<div class="yindao_inp">
				<a href="index.php/login/experience"><img src="images/fhsyb_inp.jpg" /></a>
				<img id="btnNext" class="link" src="images/bcxyb_inp.jpg" />
			</div>
			<!-- 模特卡管理 结束 -->


		</div>
		<!-- 04 结束 -->

	</form>

	<!-- 页脚 开始 -->
 <?php include 'footer.php';?>
<!-- 页脚 结束 -->
</body>
</html>
